package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xrit.sunnyday.dao.MemberMobileDao;
import com.xrit.sunnyday.dao.OrderMobileDao;
import com.xrit.sunnyday.dao.OrderSettingDao;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.Member;
import com.xrit.sunnyday.pojo.Order;
import com.xrit.sunnyday.pojo.OrderSetting;
import com.xrit.sunnyday.service.OrderMobileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Transactional
@Service(interfaceClass = OrderMobileService.class)
public class OrderMobileServiceImpl implements OrderMobileService {
    @Autowired
    private OrderMobileDao orderMobileDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberMobileDao memberMobileDao;

    @Override
    public Order submitOrder(Map<String, String> orderInfo) throws SunnydayException {
        //获取前端填的日期
        String orderDate = orderInfo.get("orderDate");
        //转换成我们要的日期格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(orderDate);
        } catch (ParseException e) {
            throw new SunnydayException("日期格式错误！");
        }

        //查询当天日期是否能预约，（是否工作）
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if(null == orderSetting){
            throw new SunnydayException("所选日期，不能预约");
        }
        if(orderSetting.getReservations() >= orderSetting.getNumber()){
            throw new SunnydayException("所选日期，预约已满");
        }

        //获取前端手机号
        String telephone = orderInfo.get("telephone");
        //根据手机号查询用户是否注册会员
        Member member = memberMobileDao.findBytelephone(telephone);
        //
        Order order = new Order();
        if(null == member){
            //不是会员，注册，添加
            Member mb = new Member();
            mb.setIdCard(orderInfo.get("idCard"));
            mb.setName(orderInfo.get("name"));
            mb.setPhoneNumber(telephone);
            mb.setSex(orderInfo.get("sex"));
            mb.setRegTime(new Date());
            memberMobileDao.add(mb);

            order.setMemberId(mb.getId());
        }else {
            order.setMemberId(member.getId());
        }

        //通过会员ID，当前时间，套餐ID查询订单，是否重复预约
        order.setOrderDate(date);
        order.setSetmealId(Integer.valueOf(orderInfo.get("setmealId")));
        Order od = orderMobileDao.findByCondition(order);
        if(null != od){
            throw new SunnydayException("请勿重复预约！");
        }
        //9.1补充订单信息
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //没重复，添加
        orderMobileDao.add(order);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return order;
    }

    @Override
    public Map<String, Object> findById4Detail(Integer id) {
        return orderMobileDao.findById4Detail(id);
    }
}
